﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.DB;
using Sector7.Medical.DAO.Standard;
using Sector7.Framework.Utility;
using Sector7.Medical.EntityType;
using Sector7.Framework.Enums;

namespace Sector7.Medical.ADDM.Controls
{
    /// <summary>
    /// InterviewerSummaryControl.xaml 的交互逻辑
    /// </summary>
    public partial class EmployeeSummaryControl : UserControl
    {
        public EmployeeSummaryControl()
        {
            InitializeComponent();
        }

        private void LoadEmployee(T7_Employee employee)
        {
            if (employee != null)
            {
                txbEmpId.Text = employee.EmpId.ToString();
                txbFullname.Text = employee.Fullname;
                txbIdentityId.Text = employee.IdentityId;
            }
            else
            {
                txbEmpId.Text = null;
                txbFullname.Text = null;
                txbIdentityId.Text = null;
            }
        }

        private void GetEmployeeList()
        {
            var employeeList = DAOFactory.GetDAO<EmployeeDAOForTemp>().ReadAll();
            ClientUtil.LoadItemsSource(dtgMain, employeeList);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            GetEmployeeList();
        }

        private void dtgMain_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadEmployee(dtgMain.SelectedItem as T7_Employee);
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            m_crud = CrudType.Create;
            gpbMain.IsEnabled = true;

            txbEmpId.Text = null;
            txbFullname.Text = null;
            txbIdentityId.Text = null;

            btnOk.IsEnabled = true;
            btnCancel.IsEnabled = true;
        }

        private void btnModify_Click(object sender, RoutedEventArgs e)
        {
            m_crud = CrudType.Update;
            gpbMain.IsEnabled = true;

            btnOk.IsEnabled = true;
            btnCancel.IsEnabled = true;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            gpbMain.IsEnabled = false;

            switch (m_crud)
            {
                case CrudType.Create:
                    var employee = new T7_Employee();
                    employee.Fullname = txbFullname.Text;
                    employee.IdentityId = txbIdentityId.Text;

                    DAOFactory.GetDAO<EmployeeDAOForTemp>().Create(employee);

                    GetEmployeeList();
                    break;
                case CrudType.Update:
                    var delta = ClientUtil.CreateDelta(typeof(T7_Employee));
                    delta.Conditions.Add("EmpId", txbEmpId.Text);
                    delta.DeltaProperties.Add("Fullname", txbFullname.Text);

                    DAOFactory.GetDAO<EmployeeDAOForTemp>().Update(delta);

                    GetEmployeeList();
                    break;
                default:
                    return;
            }

            m_crud = CrudType.Unknown;

            btnOk.IsEnabled = false;
            btnCancel.IsEnabled = false;
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            LoadEmployee(dtgMain.SelectedItem as T7_Employee);

            m_crud = CrudType.Unknown;
            gpbMain.IsEnabled = false;

            btnOk.IsEnabled = false;
            btnCancel.IsEnabled = false;
        }

        private CrudType m_crud = CrudType.Unknown;
    }
}
